package com.sky.controller.admin;


import com.sky.dto.CategoryDTO;
import com.sky.dto.CategoryPageQueryDTO;
import com.sky.entity.Category;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.CategoryService;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Update;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/admin/category")
@Slf4j
public class CategoryController {
    @Autowired
    private CategoryService categoryService;


    /**
     * 菜品分页查询
     *
     * @param categoryPageQueryDTO 菜品分页查询DTO类
     * @return ResultPage类型
     */
    @GetMapping("/page")
    @ApiOperation("菜品分业查询")
    public Result<PageResult> page(CategoryPageQueryDTO categoryPageQueryDTO) {
        log.info("categoryPageQueryDTO:", categoryPageQueryDTO);
        PageResult result = categoryService.pageQuery(categoryPageQueryDTO);
        return Result.success(result);
    }

    /**
     * 新增分类
     *
     * @param categoryDTO
     * @return
     */
    @PostMapping
    @ApiOperation("新增分类")
    public Result insertType(@RequestBody CategoryDTO categoryDTO) {
        log.info("categoryDTO:", categoryDTO);
        categoryService.insertType(categoryDTO);
        return Result.success();
    }

    @DeleteMapping
    @ApiOperation("根据id删除分类")
    public Result deleteById(Long id) {
        log.info("id:", id);
        categoryService.deleteById(id);
        return Result.success();
    }


    @PutMapping
    @ApiOperation("修改分类")
    public Result updateById(@RequestBody CategoryDTO categoryDTO) {
        log.info("categoryDTO:", categoryDTO);
        categoryService.updateById(categoryDTO);
        return Result.success();
    }

    @PostMapping("/status/{status}")
    @ApiOperation("启用停用")
    public Result<String> StartOrStop(@PathVariable("status") Integer status, Long id) {
        log.info("status,id:", status, id);
        categoryService.StartOrStop(status, id);
        return Result.success();
    }

    @GetMapping("list")
    @ApiOperation("根据类型查询分类")
    public Result<List<Category>> list(Integer type) {
        List<Category> list = categoryService.list(type);
        return Result.success(list);
    }
}
